package com.example.demo.controller;


import com.example.comon.domain.R;
import com.example.demo.domain.Vo.UploadFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;

/**
 * 文件访问路径
 * http://127.0.0.1:8089/upload/111.pdf
 */
@RestController
@RequestMapping("/uploadFile")
@Api(value = "uploadFile",tags = "文件上传")
public class UploadFileController {


    @ApiOperation("附件上传")
    @PostMapping("/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件",dataType = "MultipartFile",  allowMultiple = true,  required = true, paramType = "form"),
    })
    public R uploadFile(@RequestPart("file")  MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.') + 1);
        String newFileName = UUID.randomUUID()+"."+suffix;
        String newFilePath = "D:\\tmp\\"+newFileName;
        File desc = new File(newFilePath);
        file.transferTo(desc);

        UploadFileVo uploadFileVo = new UploadFileVo();
        uploadFileVo.setFileName(newFileName);
        uploadFileVo.setFilePath(newFilePath);
        uploadFileVo.setUrl("/upload/"+newFileName);
        return R.success(uploadFileVo);
    }
}
